package falstad;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSlider;


public class SimpleActionListener implements ActionListener{

	Container parent ;
	Maze maze ;
	JSlider slider;
	
	//Key is the key code passed to keyDown to indicate the action to be performed
	int key;
	
	SimpleActionListener(Container parent, Maze maze){
		this.parent = parent ;
		this.maze = maze ;
	}

//So that the method can access the slider object's value
	SimpleActionListener(Container parent, Maze maze, JSlider slider){
		this.parent = parent ;
		this.maze = maze ;
		this.slider = slider;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String buttonName = arg0.getActionCommand();
		//System.out.println(buttonName);
		switch(buttonName)
		{
		case Constants.BUTTONNAME_BUILD_INTERRUPT:
		{
			key = Constants.KEY_ESCAPE;
			break;
		}
		case Constants.BUTTONNAME_TERMINATE:
		{
			key = Constants.CODE_STOPPLAYING;
			break;
		}
		case Constants.BUTTONNAME_CONTINUEPLAYING:
		{
			key = Constants.CODE_CONTINUEPLAYING;
			break;
		}
		case Constants.BUTTONNAME_RETURNTOTITLESCREEN:
		{	
				key = Constants.CODE_RETURNTOTITLESCREEN;
				break;
		}
		case Constants.BUTTONNAME_PLAY:
		{
			key = Character.forDigit(slider.getValue(),10);
			break;
		}
		//System.out.println("key: "+ key);

	}
	maze.keyDown(null, key);
	parent.repaint() ;
	}
}
